package com.example.meituan.activity.user.adapter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.meituan.R;
import com.example.meituan.activity.bean.CommentBean;
import com.example.meituan.activity.bean.TypeBean;
import com.example.meituan.activity.bean.UserBean;
import com.example.meituan.dao.TypeDao;
import com.example.meituan.dao.UserDao;

import java.io.File;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder>{
    private List<CommentBean> commentBeans;
    public CommentAdapter(List<CommentBean> commentBeans){
        this.commentBeans=commentBeans;
    }
    @NonNull
    @Override
    public CommentAdapter.CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_list, parent, false);
        return new CommentAdapter.CommentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CommentAdapter.CommentViewHolder holder, int position) {
        CommentBean commentBean = commentBeans.get(position);
        holder.bind(commentBean);
    }

    @Override
    public int getItemCount() {
        return commentBeans.size();
    }

    public static class CommentViewHolder extends RecyclerView.ViewHolder {

        private TextView userName,detialsType,detialsTime,commentContext;
        private ImageView userImg,commentImg;

        public CommentViewHolder(@NonNull View itemView) {
            super(itemView);
            userName=itemView.findViewById(R.id.user_name_comment);
            userImg=itemView.findViewById(R.id.user_img_comment);
            commentImg=itemView.findViewById(R.id.comment_img);
            detialsTime=itemView.findViewById(R.id.comment_detialsTime);
            detialsType=itemView.findViewById(R.id.comment_detialsType);
            commentContext=itemView.findViewById(R.id.comment_content);
        }

        public void bind(CommentBean commentBean) {
            UserBean userBean= UserDao.getUserById(commentBean.getUserId());
            userName.setText(userBean.getsName());
            String imagePath = userBean.getsImg();
            File imageFile = new File(imagePath);
            if (imageFile.exists()) {
                Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
                if (bitmap != null) {
                    userImg.setImageBitmap(bitmap);
                }
            }
            TypeBean typeBean= TypeDao.getTypeById(commentBean.getTypeId());
            detialsType.setText(typeBean.getTypeDescription());
            Instant instant = Instant.ofEpochMilli(Long.parseLong(commentBean.getId()));
            ZoneId zoneId = ZoneId.of("Asia/Shanghai");
            ZonedDateTime zonedDateTime = instant.atZone(zoneId);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日", Locale.CHINA);
            detialsTime.setText(zonedDateTime.format(formatter));
            commentContext.setText(commentBean.getComment());
            File imageFile1 = new File(commentBean.getImg());
            if (imageFile1.exists()) {
                Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
                if (bitmap != null) {
                    commentImg.setImageBitmap(bitmap);
                }
            }
        }


    }
}
